// FileLogging.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "FileLogging.h"
#include "XmlLogging.h"

#include <comdef.h>
#include <iostream>
#include <sstream>
#include <fstream>

#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

static CLogger * FILELOGGER = NULL;
static CLogger * XMLLOGGER = NULL;

CFileLogger::CFileLogger()
: CLogger()
{

}

void CFileLogger::write(CEntry * entry)
{
  if (m_SevLevel > (int)entry->getSeverity())
    return;

  // std::string s (entry->getTimestamp());
  // s = s.substr(9, 10);
  // s += ".txt";

  std::string s(bstr_t(getLogName()) + ".txt");
  std::fstream fs(s.c_str(), std::ios_base::in | std::ios_base::out | 
                  std::ios_base::binary | std::ios_base::app);
  entry->operator <<(fs);
  fs.flush();
  fs.close();
}

CLogger * CFileLoggingFactory::getLogger()
{
  if (eXml == this->getLogtype()) {
    if (NULL == XMLLOGGER)
      XMLLOGGER = new XmlLogging;
    return XMLLOGGER;
  } else if (eFile == this->getLogtype()) {
    if (NULL == FILELOGGER)
      FILELOGGER = new CFileLogger;
    return FILELOGGER;
  } else {
    return NULL;
  }
}

